set matsize 10000
global mainpath ".."

*interact forecaster FE with output gap and inflation, start in 1993 after break in forecaster ids
use "$mainpath\data\bc_May23.dta", clear
gen ym = mofd(date)
format ym %tm

gen year=yofd(date)
keep if year>=1993

xi: reg ff i.id i.ym i.id*cpi i.ym*cpi i.id*gap i.ym*gap,  ro

save "$mainpath/tmp/id_ym_interact.dta", replace

use "$mainpath/tmp/id_ym_interact.dta", clear

gen gamma_396=_b[gap]
gen beta_396=_b[cpi]
forvalues m = 397/760 {
	gen gamma_`m'=_b[_IymXgap_`m']+_b[gap]
	gen beta_`m'=_b[_IymXcpi_`m']+_b[cpi]

}

keep if _n==1
keep gamma_* beta_*
gen count=_n
reshape long gamma_ beta_, i(count) j(ym)
format ym %tm
rename gamma_ gamma_id_ym_interact
rename beta_ beta_id_ym_interact

order ym gamma_id_ym_interact
keep ym gamma_id_ym_interact
gsort ym
save "$mainpath/output/coeffs_id_ym_interact.dta", replace

*split by inflation terciles
use "$mainpath\data\bc_May23.dta", clear
gen ym = mofd(date)
format ym %tm
gsort ym 

*start in 1993, after break in forecaster id
gen year=yofd(date)
gsort ym id h


*de-mean inflation forecast
xi: regress cpi i.ym if h==4, ro
predict cpi_demean if h==4, res

_pctile cpi_demean if h==4, p(33.333 66.667)
gen cut1=r(r1) 
gen cut2=r(r2) 

gsort ym id
gen cut1_temp=0
gen cut2_temp=0
by ym: replace cut1_temp=1 if cpi_demean<cut1&h==4
by ym: replace cut2_temp=1 if cpi_demean>cut2&h==4


by ym id: egen cut1_all=max(cut1_temp)
by ym id: egen cut2_all=max(cut2_temp)

save "$mainpath/tmp/bc_cuts.dta", replace

*run estimation for forecasters in tercile 1
use "$mainpath/tmp/bc_cuts.dta", replace
keep if cut1_all==1
gsort ym
by ym: egen no_obs=count(ff)

tempfile coeffs_tercile1
statsby _b _se rsme=e(rmse) r2=e(r2), by(ym) saving(`coeffs_tercile1', replace): areg ff cpi gap, absorb(id)

use `coeffs_tercile1', clear

rename _b_gap gamma_tercile1
format ym %tm 
keep ym gamma_tercile1
gsort ym
save "$mainpath/output/gamma_tercile1.dta", replace
*run estimation for forecasters in tercile 2
use "$mainpath/tmp/bc_cuts.dta", replace
keep if cut1_all==0&cut2_all==0
keep if id~=.

gsort ym

tempfile coeffs_tercile2
statsby _b _se rsme=e(rmse) r2=e(r2), by(ym) saving(`coeffs_tercile2', replace): areg ff cpi gap, absorb(id)

use `coeffs_tercile2', clear

rename _b_gap gamma_tercile2
format ym %tm 
keep ym gamma_tercile2
gsort ym
save "$mainpath/output/gamma_tercile2.dta", replace

*run estimation for forecasters in tercile 3
use "$mainpath/tmp/bc_cuts.dta", replace
keep if cut2_all==1
keep if id~=.

gsort ym

tempfile coeffs_tercile3
statsby _b _se rsme=e(rmse) r2=e(r2), by(ym) saving(`coeffs_tercile3', replace): areg ff cpi gap, absorb(id)

use `coeffs_tercile3', clear

rename _b_gap gamma_tercile3
format ym %tm 
keep ym gamma_tercile3
gsort ym
save "$mainpath/output/gamma_tercile3.dta", replace

rm "$mainpath/tmp/id_ym_interact.dta"
rm "$mainpath/tmp/bc_cuts.dta"